package com.thecafetechno.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Query;
import org.hibernate.Transaction;
import com.thecafetechno.university.Customer;
import com.thecafetechno.university.Address;
import com.thecafetechno.util.HibernateUtil;

import java.util.Iterator;

public class TestHibernate {
	public static void main(String[] args) {
	    Session session = null;
	    Transaction ut = null;
	    try{
            // setup JNDI
            //SessionFactory sf = HibernateUtil.getSessionFactory().getCurrentSession(); 
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            ut = session.getTransaction();
         
            ut.begin();
           // save customer 
            Customer customer = new Customer();
            customer.setName("Deepak");
            customer.setEmailAddress("surya@gmail.com");
            customer.setContactNo("91-932686876");
            Address address = new Address();
            address.setAddressLine1("xxx-street, near Surya Complex");
            address.setCity("Pune");
            address.setState("Maharashtra");
            address.setPincode(11111);
           
            customer.setAddress(address);
            address.setCustomer(customer);
            session.save(customer);
            
            
             customer = new Customer();
            customer.setName("Deepak2");
            customer.setEmailAddress("surya@gmail.com2");
            customer.setContactNo("91-9326868762222222222");
              address = new Address();
            address.setAddressLine1("xxx-street, near Surya Complex22222222");
            address.setCity("Pune2222222");
            address.setState("Maharashtra222222222");
            address.setPincode(11111);
           
            customer.setAddress(address);
            address.setCustomer(customer);
            session.save(customer);

      //fetch all customers
              
//               Query query = session.createQuery("from Customer customer");
//               for(Iterator it=query.iterate();it.hasNext();){
//                Customer customer1 = (Customer) it.next();
//                Address address1 = customer1.getAddress();
//                   System.out.println("customer ID:" + customer1.getCustomerId());
//                   System.out.println("Name: " + customer1.getName());
//                   System.out.println("address: " + address1.getAddressLine1());
//                   System.out.println(address1.getAddressId());
//                   System.out.println(address1.getAddressLine2());
//                   System.out.println(address1.getCity());
//                   System.out.println(address1.getState());
//                   System.out.println(address1.getPincode());
//                 }
            

            
            
            ut.commit();

	        
	        System.out.println("Done");
	        
	    }catch(Exception e){
	    	e.printStackTrace();
	    	try{
	    		ut.rollback();
	    	}catch(Exception e1){
	    		System.out.println("Counldn't rollback ");
	    		e1.printStackTrace();
	    	}
	        System.out.println(e.getMessage());
	        e.printStackTrace();
	    }finally{
	    	//session.close();
	    }
	  }

}

